package com.fau.sd.timer;

import java.sql.Types;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Timer;

import javax.annotation.Resource;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.sd.util.SystemConfig;
import com.fau.sd.util.UuidUtil;


/**
 * @desc 话务提醒
 * @author liy
 * @datetime 2014年12月31日,上午10:35:31
 */
@Service
@Transactional(readOnly = true)
public class TelephonistTimer {
	
	private static final Logger log = Logger.getLogger(TelephonistTimer.class);
	
	@Resource
	private JdbcTemplate jdbcTarget;
	
	/**
	 * @desc 每天0点0分30秒执行
	 * @author liy
	 * @datetime 2014年12月31日,上午10:37:29
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Scheduled(cron = "0 30 0 * * ?")
	public void timingTask() {
		Calendar c = Calendar.getInstance();
		int month = c.get(Calendar.MONTH) + 1;
		int date = c.get(Calendar.DATE);
		int week = c.get(Calendar.DAY_OF_WEEK) - 1;
		
		List<Map<String, Object>> list = jdbcTarget.queryForList("select * from medical_call_customer_reminder");
		for (Map<String, Object> map : list) {
			if(SystemConfig.WEEK_REMIND.equals(map.get("cycle").toString())) {	//每周
				String[] time = map.get("reminder_time").toString().split("\\|");
				String w = week+"";
				if(w.equals(time[0])){
					this.insertToTemp(map.get("content").toString(),map.get("hospital_id").toString(),map.get("operator_id").toString());
					Timer timer = new Timer();
					timer.schedule(new RemindTask(map.get("operator_id").toString(), map.get("content").toString()), this.getCurrDate(c,time[1]));
				}
			} else if(SystemConfig.DAY_REMIND.equals(map.get("cycle").toString())) {	//每天
				this.insertToTemp(map.get("content").toString(),map.get("hospital_id").toString(),map.get("operator_id").toString());
				String hm = map.get("reminder_time").toString();
				Timer timer = new Timer();
				timer.schedule(new RemindTask(map.get("operator_id").toString(), map.get("content").toString()), this.getCurrDate(c,hm));
			} else if(SystemConfig.MONTH_REMIND.equals(map.get("cycle").toString())) {	//每月
				String[] time = map.get("reminder_time").toString().split("\\|");
				String d = date + "";
				if(d.equals(time[0])){
					this.insertToTemp(map.get("content").toString(),map.get("hospital_id").toString(),map.get("operator_id").toString());
					Timer timer = new Timer();
					timer.schedule(new RemindTask(map.get("operator_id").toString(), map.get("content").toString()), this.getCurrDate(c,time[1]));
				}
			} else if(SystemConfig.YEAR_REMIND.equals(map.get("cycle").toString())) {	//每年
				String[] time = map.get("reminder_time").toString().split("\\|");
				String m = month + "";
				String d = date + "";
				if(m.equals(time[0]) && d.equals(time[1])) {
					this.insertToTemp(map.get("content").toString(),map.get("hospital_id").toString(),map.get("operator_id").toString());
					Timer timer = new Timer();
					timer.schedule(new RemindTask(map.get("operator_id").toString(), map.get("content").toString()), this.getCurrDate(c,time[3]));
				}
			} else if(SystemConfig.ONCE_REMIND.equals(map.get("cycle").toString())) {	//一次性
				String[] time = map.get("reminder_time").toString().split("\\|");
				String today = DateFormatUtils.format(c, "yyyy-MM-dd");
				if(today.equals(time[0])) {
					this.insertToTemp(map.get("content").toString(),map.get("hospital_id").toString(),map.get("operator_id").toString());
					Timer timer = new Timer();
					timer.schedule(new RemindTask(map.get("operator_id").toString(), map.get("content").toString()), this.getCurrDate(c,time[1]));
				}
			}
		}
	}
	
	/**
	 * @desc 获得当天的日期对象
	 * @param c 当天日期
	 * @param hm 时分
	 * @return Date
	 * @author liy
	 * @datetime 2015年1月4日,上午10:17:12
	 */
	private Date getCurrDate(Calendar c,String hm) {
		String curr = DateFormatUtils.format(c, "yyyy-MM-dd");
		curr = curr + " " + hm;
		Date date = null;
		try {
			date = DateUtils.parseDate(curr, "yyyy-MM-dd HH:mm");
		} catch (ParseException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * @desc 将符合条件的提醒保存至消息表
	 * @author liy
	 * @datetime 2015年1月4日,上午10:45:39
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	private void insertToTemp(String content,String eid,String operatorId) {
		jdbcTarget.update("insert into medical_message values(?,?,?,?,?,?) "
		, new Object[]{UuidUtil.getUuid(),content,0,eid,new Date(),operatorId}
		, new int[]{Types.VARCHAR,Types.VARCHAR,Types.INTEGER,Types.VARCHAR,Types.TIMESTAMP,Types.VARCHAR});
	}
	
}
